#ifndef __CACHEDFILEMANAGER_H__
#define __CACHEDFILEMANAGER_H__



#include "Common/Common.h"
#include "Music/MusicInfo.h"

namespace SPlayer
{
    namespace Network
    {
        using namespace Music;
        using namespace System;
        using namespace SPlayer::Common;
        ref class CachedFileManager
        {
            /*! @brief Convert a network path into local path*/
            static String ^toLocalPath(String ^filename)
            {
                return cacheDirectory + "/"+ filename;
            }
        public:
            /*! @brief Check whether the network file is downloaded or not*/
            static bool isMusicFileCached(String ^networkPath)
            {
                MusicPathInfo p(networkPath);
                if (p.isNetworkPath)
                {
                    return Common::Function::fileExist(toLocalPath(p.normalMusicPath));
                }
                return true;
            }

            /*! @brief Check whether the network file is downloaded or not*/
            static bool isLyricFileCached(String ^networkPath)
            {
                MusicPathInfo p(networkPath);
                if (p.isNetworkPath)
                {
                    return Common::Function::fileExist(toLocalPath(p.normalLyricPath));
                }
                return true;
            }

            static String ^toMusicCachedPath(String ^networkPath)
            {
                return toLocalPath(MusicPathInfo(networkPath).normalMusicPath);
            }

            static String ^toLyricCachedPath(String ^networkPath)
            {
                return toLocalPath(MusicPathInfo(networkPath).normalLyricPath);
            }
        public:
            static property String ^cacheDirectory
            {
                String ^get()
                {
                    return _fileSavePath;
                }
                void set(String ^newPath)
                {
                    if (Function::dirExist(newPath))
                    {
                        if (newPath->EndsWith("/") || newPath->EndsWith("\\"))
                        {
                            newPath = newPath->Substring(0, newPath->Length - 1);
                        }
                        _fileSavePath = newPath;
                    }
                }
            }
        private:
            // File Downloading
            static String ^_fileSavePath = "./Cache";
        };

    }
}
#endif // __CACHEDFILEMANAGER_H__